package com.spsoft.vatoutinv.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.spsoft.core.annotation.AnonymousAccess;
import com.spsoft.core.annotation.Log;
import com.spsoft.core.controller.BaseController;
import com.spsoft.core.enums.LogActionType;
import com.spsoft.core.enums.LogMenuEnum;
import com.spsoft.core.enums.ResponseEnum;
import com.spsoft.core.model.Result;
import com.spsoft.vatoutinv.model.dto.vmsfullbswjmanager.VmsFullVswjManagerPageDto;
import com.spsoft.vatoutinv.model.vo.vmsfullbswjmanager.VmsFullBswjManagerDetailVO;
import com.spsoft.vatoutinv.model.vo.vmsfullbswjmanager.VmsFullBswjManagerKprVO;
import com.spsoft.vatoutinv.model.vo.vmsfullbswjmanager.VmsFullBswjManagerPageVO;
import com.spsoft.vatoutinv.service.vmsfullbswjmanager.VmsFullBswjManagerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 数电发票板式文件管理表;数电发票板式文件管理表 前端控制器
 * </p>
 *
 * @author wangy
 * @since 2023-11-29
 */
@Api(tags = {"数电发票版式文件管理"})
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/vmsFullBswjManager")
public class VmsFullBswjManagerController extends BaseController {

    public static final String LOG_MSG = "(数电发票版式文件管理控制层)";

    private final VmsFullBswjManagerService fullBswjManagerService;

    @ApiOperation("数电发票配置管理-新增")
    @AnonymousAccess(value = "/save", method = RequestMethod.POST, permissionCode = "VMS_FULL_BSWJ_ADD")
    @Log(value = "数电发票配置管理-新增", type = LogActionType.ADD, menuName = LogMenuEnum.DIGITAL_INVOICE_FILE)
    public Result<Long> save(@RequestPart(value = "file", required = false) MultipartFile file,
                             @RequestParam(value = "whjg") String deptCode,
                             @RequestParam(value = "qydm") String qydm,
                             @RequestParam(value = "qymc") String qymc,
                             @RequestParam(value = "sydwbh") String ptbh,
                             @RequestParam(value = "bswjsczt") String bswjsczt,
                             @RequestParam(value = "fpkjzt") String fpkjzt,
                             @RequestParam(value = "bswjId", required = false) String bswjId,
                             @RequestParam(value = "nsrsbh") String nsrsbh,
                             @RequestParam(value = "nsrmc") String nsrmc,
                             @RequestParam(value = "jkkpkpr") String jkkpkpr) {
        log.info("{}新增数电发票配置管理", LOG_MSG);
        return fullBswjManagerService.fileSave(file, deptCode, qydm, qymc, ptbh, bswjsczt, fpkjzt, bswjId, nsrsbh, nsrmc, jkkpkpr);
    }


    @ApiOperation("数电发票配置管理-分页查询")
    @AnonymousAccess(value = "/page", method = RequestMethod.POST, permissionCode = "VMS_INVOICETEMPLATE")
    @Log(value = "数电发票配置管理-分页查询", type = LogActionType.SELECT, menuName = LogMenuEnum.DIGITAL_INVOICE_FILE)
    public Result<IPage<VmsFullBswjManagerPageVO>> page(@RequestBody VmsFullVswjManagerPageDto dto) {
        log.info("{}数电发票配置管理-分页查询：{}", LOG_MSG, JSONObject.toJSONString(dto));
        return fullBswjManagerService.getPage(dto);
    }

    @ApiOperation("数电发票配置管理-根据ID查看图片")
    @AnonymousAccess(value = "/getFileById", method = RequestMethod.POST, permissionCode = "VMS_INVOICETEMPLATE")
    @Log(value = "数电发票配置管理-根据ID查看图片", type = LogActionType.SELECT, menuName = LogMenuEnum.DIGITAL_INVOICE_FILE)
    public Result<String> getFileById(@RequestBody JSONObject jsonObject) {
        if (jsonObject == null || StringUtils.isBlank(jsonObject.getString("id"))) {
            return Result.ERROR(ResponseEnum.ILLEGAL_ARGUMENT_ERROR);
        }
        log.info("{}数电发票配置管理-根据ID查看图片：{}", LOG_MSG, jsonObject.getString("id"));
        return fullBswjManagerService.getFileById(jsonObject.getString("id"));
    }

    @ApiOperation("数电发票配置管理-根据ID删除")
    @AnonymousAccess(value = "/deleteFileById", method = RequestMethod.POST, permissionCode = "VMS_FULL_BSWJ_ADD")
    @Log(value = "数电发票配置管理-根据ID删除", type = LogActionType.SELECT, menuName = LogMenuEnum.DIGITAL_INVOICE_FILE)
    public Result<Boolean> deleteFileById(@RequestBody JSONObject jsonObject) {
        if (jsonObject == null || StringUtils.isBlank(jsonObject.getString("id"))) {
            return Result.ERROR(ResponseEnum.ILLEGAL_ARGUMENT_ERROR);
        }
        log.info("{}数电发票配置管理-根据ID删除：{}", LOG_MSG, jsonObject.getString("id"));
        return fullBswjManagerService.deleteFileById(jsonObject.getString("id"));
    }

    /**
     * 修改状态
     */
    @ApiOperation("数电发票配置管理-修改状态")
    @Log(value = "数电发票配置管理-修改状态", type = LogActionType.UPDATE, menuName = LogMenuEnum.BUSINESS_SWITCH)
    @AnonymousAccess(value = "/updateStatus", method = RequestMethod.POST, permissionCode = "VMS_FULL_BSWJ_ADD")
    public Result<String> updateStatus(@RequestParam String id, @RequestParam String type, @RequestParam String status) {
        log.info("{}数电发票配置管理-修改状态：id:{},type:{},status:{}", LOG_MSG, id, type, status);
        // 01 版式文件生成状态 02 发票开具状态 03 启用状态
        if (!"01".equals(type) && !"02".equals(type) && !"03".equals(type)) {
            return Result.ERROR(ResponseEnum.BUSINESS_SWITCH_ERROR_02);
        }
        return fullBswjManagerService.updateStatus(id, type, status);
    }

    /**
     * 根据版式文件ID获取详情
     */
    @ApiOperation("数电发票配置管理-根据ID获取详情")
    @Log(value = "数电发票配置管理-根据ID获取详情", type = LogActionType.SELECT, menuName = LogMenuEnum.DIGITAL_INVOICE_FILE)
    @AnonymousAccess(value = "/getInfoByBswjId", method = RequestMethod.POST, permissionCode = "VMS_INVOICETEMPLATE")
    public Result<VmsFullBswjManagerDetailVO> getInfoByBswjId(@RequestBody JSONObject jsonObject) {
        if (jsonObject == null || StringUtils.isBlank(jsonObject.getString("bswjId"))) {
            return Result.ERROR(ResponseEnum.ILLEGAL_ARGUMENT_ERROR);
        }
        log.info("{}数电发票配置管理-根据ID获取详情：id:{}", LOG_MSG, jsonObject.getString("bswjId"));
        return fullBswjManagerService.getDetailByBswjId(jsonObject.getString("bswjId"));
    }

    /**
     * 根据机构获取接口开票人列表
     */
    @ApiOperation("数电发票配置管理-根据机构获取接口开票人列表")
    @Log(value = "数电发票配置管理-根据机构获取接口开票人列表", type = LogActionType.SELECT, menuName = LogMenuEnum.DIGITAL_INVOICE_FILE)
    @AnonymousAccess(value = "/getJkkprByWhjg", method = RequestMethod.POST, permissionCode = "VMS_INVOICETEMPLATE")
    public Result<List<VmsFullBswjManagerKprVO>> getJkkprByWhjg(@RequestBody JSONObject jsonObject) {
        if (jsonObject == null || StringUtils.isBlank(jsonObject.getString("whjg"))) {
            return Result.ERROR(ResponseEnum.ILLEGAL_ARGUMENT_ERROR);
        }
        log.info("{}数电发票配置管理-根据机构获取接口开票人列表：维护机构:{}", LOG_MSG, jsonObject.getString("whjg"));
        return fullBswjManagerService.getJkkprByWhjg(jsonObject.getString("whjg"));
    }

}

